//创建参数配置通用控制器
for (var i = 1; i < menus[0].children.length; i++) {
  (function (page) {
    app.controller(page.path + 'Ctrl', function ($scope, $http, $location) {
      //页面通用初始化
      pageInit('module');
      //左侧导航用
      menuInit($scope, $http);
      $scope.menuIndex = page.menuIndex;

      //获取参数配置
      if (page.path == "rmm") {
        configInit($scope, $http, page.path, $location, function () {
          //备份
          var configsBackup = JSON.parse(JSON.stringify($scope.configs));
          //监视rmm部署模式
          var index = getIndexOfConfig($scope, 'rmm.mode', 'rmm');
          $scope.$watch('configs[' + index[0] + '].appList[' + index[1] + '].value', function (newValue, oldValue) {
            //复原/因为删除项是不可逆操作
            $scope.configs = JSON.parse(JSON.stringify(configsBackup));
            $scope.configs[index[0]].appList[index[1]].value = newValue;
            //动态改变配置项
            if (newValue == 1) {
              deleteConfigs($scope, ["rmm.timer.server", "deploy.event-node", "deploy.event-node|(event[R_TRD_REALTIME])", "deploy.event-node|(event[else])"]);
            } else if (newValue == 2) {
              deleteConfigs($scope, ["rmm.mode1.server", "deploy.event-node|(event[R_TRD_REALTIME])", "deploy.event-node|(event[else])"]);
            } else if (newValue == 3) {
              deleteConfigs($scope, ["rmm.mode1.server", "deploy.event-node"]);
            }
          }, true);
        });
      } else if (page.path == "cas") {

        //第三方免登录验证
        function changeThirdSsoEnable(temp) {
          if (temp.appList.length > 1) {
            var allFalse = true;
            for (var i in temp.appList) {
              if (temp.appList[i].value == "false") {
                var name = temp.appList[i].name;
                //动态删除配置项
                deleteConfigs($scope, ["com.hundsun.uam.login.thirdLoginUrl", "com.hundsun.uam.login.thirdLoginParams"], name);
              } else {
                allFalse = false;
              }
            }
            if (allFalse) {
              deleteConfigs($scope, ["com.hundsun.uam.login.thirdLoginUrl", "com.hundsun.uam.login.thirdLoginParams"]);
            }
          } else {
            if (temp.appList[0].value == "false") {
              deleteConfigs($scope, ["com.hundsun.uam.login.thirdLoginUrl", "com.hundsun.uam.login.thirdLoginParams"]);
            }
          }
        }

        //单点登录
        function changeCasEnabled(temp) {
          var allFalse = true;
          for (var i in temp.appList) {
            if (temp.appList[i].value == "true") {
              allFalse = false;
            }
          }
          if (allFalse) {
            deleteConfigs($scope, ["cas.server"]);
          }
        }

        //init回调
        configInit($scope, $http, page.path, $location, function () {
          //备份
          var configsBackup = JSON.parse(JSON.stringify($scope.configs));

          $scope.$watch(function () {
            //监视是否启用第三方免登录验证
            var index1 = getIndexOfConfig($scope, 'com.hundsun.uam.login.thirdSsoEnable');
            //监视是否启用单点登录
            var index2 = getIndexOfConfig($scope, 'client.casEnabled');
            return [$scope.configs[index1[0]], $scope.configs[index2[0]]];
          }, function (newValue, oldValue) {

            //复原/因为删除项是不可逆操作
            $scope.configs = JSON.parse(JSON.stringify(configsBackup));

            //第三方免登录验证
            var index1 = getIndexOfConfig($scope, 'com.hundsun.uam.login.thirdSsoEnable');
            $scope.configs[index1[0]] = newValue[0];
            changeThirdSsoEnable(newValue[0]);

            //单点登录
            var index2 = getIndexOfConfig($scope, 'client.casEnabled');
            $scope.configs[index2[0]] = newValue[1];
            changeCasEnabled(newValue[1]);

          }, true);
        });
      } else if (page.path == "msg") {
        configInit($scope, $http, page.path, $location, function () {
          //监视消息提醒开关
          var index1 = getIndexOfConfig($scope, 'enable_flag');
          //监视系统许可证过期提醒任务开关
          var index2 = getIndexOfConfig($scope, 'license_check_flag');
          $scope.$watch(function () {
            return [$scope.configs[index1[0]], $scope.configs[index2[0]]];
          }, function (newValue, oldValue) {

            //消息提醒开关
            // 数据初始化时校验互斥选项，如果同事选中，去除1.0
            if (oldValue[0].appList == newValue[0].appList) {
              if (newValue[0].appList.length > 1)
                if (newValue[0].appList[0].value == "1" && newValue[0].appList[1].value == "1")
                  $scope.configs[index1[0]].appList[0].value = "0";
            }
            $scope.configs[index1[0]] = newValue[0];
            if (newValue[0].appList.length > 1) {
              if (oldValue[0].appList[0].value == "0" && newValue[0].appList[0].value == "1")
                $scope.configs[index1[0]].appList[1].value = "0";
              if (oldValue[0].appList[1].value == "0" && newValue[0].appList[1].value == "1")
                $scope.configs[index1[0]].appList[0].value = "0";
            }

            //系统许可证过期提醒任务开关
            // 数据初始化时校验互斥选项，如果同事选中，去除1.0
            if (oldValue[1].appList == newValue[1].appList) {
              if (newValue[1].appList.length > 1)
                if (newValue[1].appList[0].value == "1" && newValue[1].appList[1].value == "1")
                  $scope.configs[index2[0]].appList[0].value = "0";
            }
            $scope.configs[index2[0]] = newValue[1];
            if (newValue[1].appList.length > 1) {
              if (oldValue[1].appList[0].value == "0" && newValue[1].appList[0].value == "1")
                $scope.configs[index2[0]].appList[1].value = "0";
              if (oldValue[1].appList[1].value == "0" && newValue[1].appList[1].value == "1")
                $scope.configs[index2[0]].appList[0].value = "0";
            }
          }, true);
        });
      } else if (page.path == "installmodel") {
        configInit($scope, $http, page.path, $location, function () {
          //备份
          var configsBackup = JSON.parse(JSON.stringify($scope.configs));
          //监视是否使用nginx
          var index1 = getIndexOfConfig($scope, 'nginx');
          var index2 = getIndexOfConfig($scope, 'nginx.apps');
          var index3 = getIndexOfConfig($scope, 'nginx.db.multi');
          var index4 = getIndexOfConfig($scope, 'nginx.db.current');
          $scope.$watch(function () {
            var arr = [];
            if (index1) arr.push($scope.configs[index1[0]]);
            if (index2) arr.push($scope.configs[index2[0]]);
            if (index3) arr.push($scope.configs[index3[0]]);
            if (index4) arr.push($scope.configs[index4[0]]);
            return arr;
          }, function (newValue, oldValue) {
            //复原/因为删除项是不可逆操作
            $scope.configs = JSON.parse(JSON.stringify(configsBackup));
            // 是否使用nginx统一入口
            $scope.configs[index1[0]] = newValue[0];
            // 统一入口的应用选择
            if (newValue[1]) {
              newValue[1].trueNum = 0;
              $scope.configs[index2[0]] = newValue[1];
              for (var ii = 0; ii < newValue[1].boxGroup.length; ii++)
                if (newValue[1].boxGroup[ii].value)
                  newValue[1].trueNum++;
            }
            // 是否是多数据库实例
            if (newValue[2]) {
              $scope.configs[index3[0]] = newValue[2];
            }
            // 当前数据库是否是主数据库
            if (newValue[3]) {
              $scope.configs[index4[0]] = newValue[3];
            }
            //动态改变配置项
            // 是否使用nginx统一入口
            if (newValue[0].value == "false") {
              deleteConfigs($scope, ["nginx.port", "nginx.apps", "nginx.db.multi", "nginx.db.current", "nginx.db.master.url", "nginx.db.master.password", "nginx.db.other.urls", "nginx.db.other.passwords"]);
            }
            // 统一入口的应用选择
            if (newValue[1] && newValue[1].trueNum <= 1) {
              deleteConfigs($scope, ["nginx.db.multi", "nginx.db.current", "nginx.db.master.url", "nginx.db.master.password", "nginx.db.other.urls", "nginx.db.other.passwords"]);
            }
            // 是否是多数据库实例
            if (newValue[2] && newValue[2].value == "false") {
              deleteConfigs($scope, ["nginx.db.current", "nginx.db.master.url", "nginx.db.master.password", "nginx.db.other.urls", "nginx.db.other.passwords"]);
            }
            // 当前数据库是否是主数据库
            if (newValue[3] && newValue[3].value == "true") {
              deleteConfigs($scope, ["nginx.db.master.url", "nginx.db.master.password"]);
            }
          }, true);

        });
      } else if (page.path == "web") {
        configInit($scope, $http, page.path, $location, function () {

          $http.get('/deploy/param/installmodel/init').then(function (response) {
            if (response.status == 200) {
              var message = response.data;
              if (message.code == 0) {
                var installmodelConfigs = message.data;
                for (var j = 0; j < installmodelConfigs.length; j++) {
                  if (installmodelConfigs[j].name == "hadoop") {
                    var hide = !(installmodelConfigs[j].value == "true");
                  }
                }
                if (hide)
                  deleteConfigs($scope, ["hadoop.server"]);
              } else {
                $.showNotice(message.msg, "danger");
              }
            }
          }).catch(function (response) {
            $.showNotice(response.status + " error：" + response.data.message, "danger");
          });

        });
      } else
        configInit($scope, $http, page.path, $location);
    });
  })(menus[0].children[i]);
}